<template>
  <div class="main">
    <center>
      <el-button
        class="addbutton"
        @click="dialogFormVisible1 = true"
        type="primary"
        icon="el-icon-plus"
        >增加</el-button
      >
      <!-- 增加时候的弹出层 -->
      <el-dialog
        title="添加用户"
        :visible.sync="dialogFormVisible1"
        width="500px"
        top="200px"
      >
        <el-form :model="formadd">
          <el-form-item label="角色" :label-width="formLabelWidth">
            <el-select v-model="formadd.role_id" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth">
            <el-input v-model="formadd.account" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="formadd.username" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="formadd.password" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table
        v-loading="loading"
        :data="
          tableData.filter(
            data =>
              !search ||
              data.username.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 80%"
        border
        height="600"
      >
        <el-table-column label="序号" width="180" align="center">
          <template slot-scope="scope">
            <span v-text="getIndex(scope.$index)"></span>
          </template>
        </el-table-column>
        <el-table-column label="账号" prop="account"> </el-table-column>
        <el-table-column label="姓名" prop="username"> </el-table-column>
        <el-table-column label="密码" prop="password"> </el-table-column>
        <el-table-column
          label="修改时间"
          prop="alter_time"
          :formatter="dataFormat"
        >
        </el-table-column>
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >

            <!-- 编辑时候的弹出层 -->
            <el-dialog
              title="用户修改"
              :visible.sync="dialogFormVisible"
              width="500px"
              top="200px"
            >
              <el-form :model="form">
                <el-form-item label="序号" :label-width="formLabelWidth">
                  <el-input v-model="form.id" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="账号" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.account"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.username"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button
                  type="primary"
                  @click="handleSet(scope.$index, scope.row)"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.totalRow"
      >
      </el-pagination>
    </center>
  </div>
</template>

<script>
import { getUserList } from "@/api/user";
import { deleteUser } from "@/api/user";
import { editUser } from "@/api/user";
import { addUser } from "@/api/user";
import moment from "moment";
export default {
  data() {
    return {
      tableData: [],
      search: "",
      pageNumber: 1, //当前页
      pageSize: 10, //每页大小
      totalRow: 0, //总条数
      totalPage: 0, //总页数
      input: "",
      loading: true,

      dialogFormVisible: false, //编辑弹出层默认为关闭
      form: {
        account: "",
        username: "",
        id: ""
      },
      formLabelWidth: "50px",

      //增加时候的弹出层
      dialogFormVisible1: false, //编辑弹出层默认为关闭
      formadd: {
        account: "",
        username: "",
        role_id: "",
        password: ""
      },

      //角色选择
      options: [
        {
          value: "管理员",
          label: "管理员"
        },
        {
          value: "卡口",
          label: "卡口"
        },
        {
          value: "车管所",
          label: "车管所"
        },
        {
          value: "警察",
          label: "警察"
        }
      ]
    };
  },

  methods: {
    //增加
    addUser() {
      var params = {
        account: this.formadd.account,
        username: this.formadd.username,
        password: this.formadd.password,
        role_id: this.formadd.role_id
      };
      addUser("/user/addUser", params).then(res => {
        if (res.code === 200) {
          this.$message("保存成功！");
          location.reload();
        } else {
          this.$message(res.data);
        }
      });
    },
    //时间格式化
    dataFormat(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    //删除
    handleDelete(index, row) {
      // console.log(index, row);
      // console.log("删除的信息为" + index + row)
      deleteUser("/user/deleteUser", row).then(res => {
        if (res.code === 200) {
          this.$message("删除成功！");
        } else {
          this.$message(res.data);
        }
      });
    },
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true; //开启弹出层
      this.form.account = row.account;
      this.form.username = row.username;
      this.form.id = row.id;
    },
    handleSet(index, row) {
      var params = {
        account: this.form.account,
        username: this.form.username,
        id: this.form.id
      };
      editUser("/user/editUser", params).then(res => {
        if (res.code === 200) {
          this.$message("修改成功！");
          this.loading = false;
          this.dialogFormVisible = false;
          location.reload();
        } else {
          this.$message(res.data);
        }
      });
    },
    //分页点击方法
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageNumber = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    findAll(params) {
      getUserList("/user/getUserList", params).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.totalRow = res.data.totalRow;
          // console.log(this.tableData.length)
          this.loading = false;
        } else if (res.code === 500) {
          this.$message(res.data);
        }
      });
    },
    //添加表格序号
    getIndex($index) {
      return (this.pageNumber - 1) * this.pageSize + $index + 1;
    }
  },
  //初始化的时候初始化表格大小
  created() {
    var params = {
      pageNumber: this.pageNumber,
      pageSize: this.pageSize
    };
    this.findAll(params);
  }
};
</script>

<style scoped>
.search {
  /* float: left; */
  margin-left: 145px;
}
.searchinput {
  float: right;
  width: 60%;
}
.elrow {
  margin-top: 10px;
}
.addbutton {
  margin-left: -1060px;
}
.main {
  height: 700px;
}
</style>
